Ext.namespace('Ext.ux', 'Ext.ux.plugins');

Ext.ux.plugins.HtmlEditorImageInsert = function(config) {

    config = config || {};

    Ext.apply(this, config);

    this.init = function(htmlEditor) {
	this.editor = htmlEditor;
	this.editor.on('render', onRender, this);
    };

    this.imageInsert = function() {
	var range,mailer_id;
	if (Ext.isIE) range = this.editor.doc.selection.createRange();
		
	var grid= Ext.getCmp(Portal.mailerScreen.id).gridMailer;
	if(grid.selModel.getCount()!=1) {	    
		mailer_id= 0;
	} else {
	    mailer_id=grid.selModel.getSelected().id;
	}
	
	var images = new Ext.data.Store({
	    proxy: new Ext.data.HttpProxy({
		url: 'mailer/bringAttach/' + Math.random()		
	    }),
	    baseParams : {  type : "image" , mailer_id: mailer_id },
	    remoteSort: true,
	    sortInfo: { field: 'name', direction: 'asc' },
	    autoLoad: {
                    params: { start: 0, limit: 20 }
            },
	    reader: new Ext.data.JsonReader({
		totalProperty: 'total',
		root: 'DS_IMAGE',
		id: 'id',
		fields: ['id', 'name']
	    })

	});

	var imagesGrid = new Ext.grid.GridPanel({
	    store: images,
	    cm: new Ext.grid.ColumnModel([{
		//id: 'image',
		header: "Imagini",
		sortable: true,
		dataIndex: 'name',
		width: 50,
		renderer: Portal.renderer.text2thumb
	    },  {
		//id: 'name',
		header: "Nume",
		sortable: true,
		dataIndex: 'name'
	    }]),
	    viewConfig: {
		forceFit: true
	    },
	    scope: this,
	    autoWidth: true,
	    layout: 'fit',
	    region: 'center',
	    height: 360,
	    //width:600,	    
	    autoScroll: true,
	    bbar: new Ext.PagingToolbar({
		beforePageText: 'Pagina&nbsp;',
		afterPageText: 'din {0}',
		firstText: 'Prima pagina',
		lastText: 'Ultima pagina',
		nextText: 'Pagina urmatoare',
		prevText: 'Pagina precedenta',
		pageSize: 20,
		store: images,
		displayInfo: true,
		displayMsg: 'Contact {0} - {1} din {2}',
		emptyMsg: "No records",
		plugins: new Ext.ux.Andrie.pPageSize({
		    beforeText: '',		   
		    comboCfg: {
			width: 50
		    }
		})
	    })

	});

	imagesGrid.on("rowclick",function(imagesGrid, row, e) {
	    var record = imagesGrid.getStore().getAt(row);
	    var fullname = record.get("name");

	    if (Ext.isIE) range.select();
	    this.editor.relayCmd('insertimage', Portal.renderer.name2path(fullname));
	    window.close();

	},
	this);

	var window = new Ext.Window({
	    title: '',
	    width: 600,
	    height: 400,
	    scope: this,
	    resizable: false,
	    plain: true,
	    bodyStyle: 'padding:5px;',
	    buttonAlign: 'right',
	    items: [imagesGrid]
	    /*,
					  buttons:[{
					      text: 'Close',
					      handler:function(){
						window.close();
					      }
					  }]*/

	});

	window.show.defer(200, window);

    }

    function onRender() {
	if (!Ext.isSafari) {
	    this.editor.tb.add({
		itemId: 'htmlEditorImage',
		iconCls: 'icon-picture',
		enableToggle: false,
		scope: this,
		handler: function() {
		    this.imageInsert();
		},
		clickEvent: 'mousedown',
		tabIndex: -1
	    });
	}
    }

}